package com.farm.template.service.impl;

import com.farm.template.domain.TemplateDir;
import com.farm.template.domain.TemplateFile;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;
import com.farm.template.dao.TemplatedirDaoInter;
import com.farm.template.dao.TemplatefileDaoInter;
import com.farm.template.service.TemplatedirServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import com.farm.cbtg.domain.Appcatalog;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：模板目录服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class TemplatedirServiceImpl implements TemplatedirServiceInter {
	@Resource
	private TemplatedirDaoInter templatedirDaoImpl;
	@Resource
	private TemplatefileDaoInter templatefileDaoImpl;
	private static final Logger log = Logger.getLogger(TemplatedirServiceImpl.class);

	@Override
	@Transactional
	public TemplateDir insertTemplatedirEntity(TemplateDir entity, LoginUser user) {
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCuser(user.getId());
		entity.setEuser(user.getId());
		entity.setCode(entity.getName());
		entity.setTreecode(entity.getTemplateid());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		if (entity.getParentid() == null) {
			entity.setParentid("NONE");
		}
		entity.setTreecode("NONE");
		entity = templatedirDaoImpl.insertEntity(entity);
		initTreeCode(entity.getId());
		return entity;
	}

	private void initTreeCode(String treeNodeId) {
		TemplateDir node = getTemplatedirEntity(treeNodeId);
		if (node.getParentid().equals("NONE")) {
			node.setTreecode(node.getId());
		} else {
			node.setTreecode(templatedirDaoImpl.getEntity(node.getParentid()).getTreecode() + node.getId());
		}
		templatedirDaoImpl.editEntity(node);
	}

	@Override
	@Transactional
	public TemplateDir editTemplatedirEntity(TemplateDir entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		TemplateDir entity2 = templatedirDaoImpl.getEntity(entity.getId());
		// entity2.setEuser(user.getId());
		// entity2.setEusername(user.getName());
		// entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setTemplateid(entity.getTemplateid());
		entity2.setTreecode(entity.getTreecode());
		entity2.setParentid(entity.getParentid());
		entity2.setSort(entity.getSort());
		entity2.setName(entity.getName());
		entity2.setCode(entity.getName());
		entity2.setPcontent(entity.getPcontent());
		entity2.setPstate(entity.getPstate());
		entity2.setCtime(entity.getCtime());
		entity2.setEtime(entity.getEtime());
		entity2.setCuser(entity.getCuser());
		entity2.setEuser(entity.getEuser());
		entity2.setId(entity.getId());
		templatedirDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteTemplatedirEntity(String id, LoginUser user) {
		// 目录有子目录不能上传
		{
			List<TemplateDir> types = templatedirDaoImpl
					.selectEntitys(DBRuleList.getInstance().add(new DBRule("PARENTID", id, "=")).toList());
			if (types.size() > 0) {
				throw new RuntimeException("目录下还有目录，请先删除子目录！");
			}
		}
		// 目录有文件不能上传
		{
			List<TemplateFile> types = templatefileDaoImpl
					.selectEntitys(DBRuleList.getInstance().add(new DBRule("DIRID", id, "=")).toList());
			if (types.size() > 0) {
				throw new RuntimeException("目录下还有文件，请先删除目录文件！");
			}
		}
		templatedirDaoImpl.deleteEntity(templatedirDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public TemplateDir getTemplatedirEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return templatedirDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createTemplatedirSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "CBTG_TEMPLATE_DIR",
				"ID,TEMPLATEID,TREECODE,PARENTID,SORT,NAME,CODE,PCONTENT,PSTATE,CTIME,ETIME,CUSER,EUSER");
		return dbQuery;
	}

	@Override
	@Transactional
	public List<TemplateDir> getTemplateDirs(String templateid) {

		List<TemplateDir> dirs = templatedirDaoImpl
				.selectEntitys(DBRuleList.getInstance().add(new DBRule("TEMPLATEID", templateid, "=")).toList());

		dirs.sort(new Comparator<TemplateDir>() {
			@Override
			public int compare(TemplateDir o1, TemplateDir o2) {
				int len = o1.getTreecode().length() - o2.getTreecode().length();
				return len == 0 ? (o1.getSort() - o2.getSort()) : len;
			}
		});

		return dirs;
	}

}
